package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师后台 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2021-07-02
 */
//@CrossOrigin
@Api(tags = "讲师后台 前端控制器")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    /**
     * 显示所有讲师列表
     * @return
     */
    @ApiOperation("显示所有讲师列表")
    @GetMapping("/list")
    public R listAll(){

        System.out.println(log);
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list).message("获取讲师列表成功");

    }

    /**
     *根据id删除讲师
     * @return
     */
    @ApiOperation(value = "根据id删除讲师",notes = "根据id删除讲师,逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value = "讲师id",required = true)
            @PathVariable String id){
        //删除头像
        boolean b = teacherService.removeAvatarById(id);
        if (!b){
//            log.warn("讲师头像删除失败,讲师id："+ id);
        }

        //删除讲师
       boolean result = teacherService.removeById(id);
       if (result){
           return R.ok().message("删除成功");
       }else{
           return R.error().message("删除失败");
       }
    }

    /**
     *根据id删除讲师
     * @return
     */
    @ApiOperation(value = "根据id批量删除讲师",notes = "根据id批量删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "讲师id列表",required = true)
            @RequestBody List<String> idList){
        boolean result = teacherService.removeByIds(idList);
        if (result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("分页条件查询")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(value = "讲师查询对象")
                    TeacherQuery teacherQuery) {

        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);
        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }
    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }
    @ApiOperation("根据id获取讲师信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if(teacher != null){
            return R.ok().message("查询成功").data("item", teacher);
        }else{
            return R.error().message("查询失败");
        }
    }
}


